package manage;

import login.*;

import java.time.LocalDate;

import java.time.LocalTime;
import java.util.*;

public class Admin implements ajiekou<User> {

    static Map<String,User>usermap = new HashMap<>();
    static List<vegetableList>dish =new ArrayList<>();
    static List<User>list =new ArrayList<>();
    //static Map<String, Order> ordermap=new HashMap<>();
    static List<Order> orderlist = new ArrayList<>();

    User u=new User();
    OrderSys o = new OrderSys();
    DishSys d = new DishSys();
    Userlogin tolUser = new Userlogin();


    public User getU() {
        return u;
    }

    public void setU(User u) {
        this.u = u;
    }

    public static List<User> getList() {
        return list;
    }

    public static void setList(List<User> list) {
        Admin.list = list;
    }

    public void initForTest()//***********************************************only for text
    {
        Userlogin login=new Userlogin();
        User tu=new User();
        tu=new User("1","小a","管理员","a",0.0,0.0);
        insert(tu);login.regist(tu);//录入信息
        tu=new User("2","小b","商家","b",0.0,0.0);
        insert(tu);login.regist(tu);
        tu=new User("3","小c","商家","c",0.0,0.0);
        insert(tu);login.regist(tu);
        tu=new User("4","小d","用户","d",0.0,0.0);
        insert(tu);login.regist(tu);
        tu=new User("5","小e","用户","e",0.0,0.0);
        insert(tu);login.regist(tu);
        tu=new User("6","小f","用户","f",0.0,0.0);
        insert(tu);login.regist(tu);


        LocalDate date=LocalDate.now();
            //String dId, String dame, String dType, double dPrice, int dNum, LocalDate dTime
        //String dId, String dame, String dType, LocalDate dTime, double dPrice, int dNum, int dSum, User cook
        vegetableList vt=new vegetableList("1","麻辣香锅","rice",20,120,date,10,findByID("2"));
        d.insert(vt);
        vt=new vegetableList("2","喷射套餐","shit",10,120,date,10,findByID("3"));
        d.insert(vt);
        vt=new vegetableList("3","兰州拉面","noddles",20,120,date,10,findByID("2"));
        d.insert(vt);
        vt=new vegetableList("4","沙县蒸饺","dumplings",30,120,date,10,findByID("3"));
        d.insert(vt);
        vt=new vegetableList("5","黄焖鸡米饭","rice",40,120,date,10,findByID("2"));
        d.insert(vt);
        vt=new vegetableList("6","上流的翔","shit",50,120,date,10,findByID("3"));
        d.insert(vt);


        LocalTime time=LocalTime.now();

//String oId, LocalTime oTime, vegetableList dishes, int oNumber, Double discount, Double tolPrice, User ctm, String status, User slr

        Order od=new Order("11",time,d.findByID("1"),1,1.0,20.0,findByID("4"),"已付款",findByID("2"));
        o.insert(od);
        od=new Order("21",time,d.findByID("2"),2,0.0,20.0,findByID("4"),"已付款",findByID("3"));
        o.insert(od);
        od=new Order("31",time,d.findByID("3"),3,10.0,80.0,findByID("5"),"已完成",findByID("2"));
        o.insert(od);
        od=new Order("41",time,d.findByID("4"),1,0.0,30.0,findByID("5"),"已付款",findByID("3"));
        o.insert(od);
        od=new Order("51",time,d.findByID("5"),10,10.0,390.0,findByID("6"),"已完成",findByID("2"));
        o.insert(od);
        od=new Order("61",time,d.findByID("6"),2,0.0,100.0,findByID("6"),"已付款",findByID("3"));
        o.insert(od);



    }
    //显示所有商家
    public List<User> findSlr() {

        Set<String>keys=usermap.keySet();//通过键来找键值

        List<User>ulist=new ArrayList<>();


        for(String key:keys){
            {
                if(Objects.equals(usermap.get(key).getID(),"商家")){

                    ulist.add(usermap.get(key));
                }

            }
        }
        if(ulist.size()>0) {
            return ulist;
        }
        else {
            return null;
        }
    }
    //添加客户
    @Override//*****************************************************添加用户
    public void insert(User user) {
        usermap.put(user.getuID(),user);
        list.add(user);
    }

    //查看客户列表
    @Override
    public List<User> findall() {

        Set<String>keys=usermap.keySet();//通过键来找键值

        List<User>userList=new ArrayList<>();
        for(String key:keys){
            {
                userList.add(usermap.get(key));
            }
        }
        return userList;
    }


    //通过id查找对应的用户
    public void showUserById()
    {
        System.out.println("请输入您要查询用户的id");
        String uid=sc.next();
        System.out.println(findByID(uid));

    }
    @Override
    public User findByID(String id) {

        if(usermap.get(id)==null)
        {
            return null;
        }
        else
        {
            return usermap.get(id);
        }
    }

    public void delUser()
    {
        showUsers();
        System.out.println("请输入要删除的用户id：");
        String uid=sc.next();
        delete(uid);
    }
    //删除指定用户信息
    @Override
    public void delete(String id) {

        if(usermap.get(id)==null)
        {
            System.out.println("找不到该id对应的用户信息！");
        }
        else{
            System.out.println(usermap.get(id)+"已删除！");
            usermap.remove(id);
        }
    }

    //修改密码操作捏
    public void changePassword(String id)
    {
        User user=findByID(id);
        if(user==null)
        {
            System.out.println("没有该用户");
        }
        else {
            System.out.println("请输入旧的密码：");
            String oldpwd = sc.next();
            if (Objects.equals(oldpwd, user.password)) {
                while (true) {
                    System.out.println("请输入新密码：");
                    String newpwd0 = sc.next();
                    System.out.println("请再次输入新密码：");
                    String newpwd1 = sc.next();
                    if (Objects.equals(newpwd0, newpwd1)) {
                        user.setPassword(newpwd0);
                        break;
                    } else {
                        System.out.println("密码输入错误，请重新输入！");
                    }
                }

            }//密码确认以及报错处理******************************************************************************************
            else {
                System.out.println("密码输入错误！");
            }
        }
    }
    Scanner sc = new Scanner(System.in);

    //上面是自己的功能，下面是搞所有的功能

    //从登录注册中获取用户信息添加到u中
    public void getUsers() {

        setList( tolUser.getarr());

        System.out.println("***************************");
        for (User user : list) {
            usermap.put(user.getuID(),user);//添加到map中，特别重要********************************************************************
            System.out.println(user);//打印录入的用户信息
        }
        System.out.println("********for text************");
    }

    //自己添加客户
    public void addUser() {
        User user = new User();
        System.out.println("请输入您想要的ID号：");
        String uID = sc.next();
        User ur=new User();
        while (true) {//用来查重复

            boolean pd = true;
            user = findByID(uID);

            if (user != null) {
                pd = false;
            }

            if (pd) {
                ur.setuID(uID);//设置uID
                break;
            } else {
                uID = sc.next();
            }
        }
        System.out.println("请输入您的用户名/邮箱：");
        String username = sc.next();
        ur.setUsername(username);

        System.out.println("请输入您的用户密码：");
        String password = sc.next();
        ur.setPassword(password);

        System.out.println("请输入您的用户类别：");
        System.out.println("1.管理员注册  2.用户注册  3.商家注册 ");

        int choice1 = sc.nextInt();
        switch (choice1) {
            case 1:
                ur.setID("管理员");
                break;
            case 2:
                ur.setID("用户");
                break;
            case 3:
                ur.setID("商家");
                break;
            default:
                break;
        }
        insert(ur);
        System.out.println("添加成功！");
    }

    //查看所有用户
    public void showUsers()
    {
        //System.out.println(u.findall());

        list = findall();
        if(list==null){
            System.out.println("找不到该id对应的用户信息！");
        }else{
            for(User user:list)
            {
                System.out.println(user);
            }
        }

    }

    public void showOrder()
    {

        List<Order>oList=o.findall();

        for(Order order:oList)
        {
            System.out.println(order);
        }

    }


    //根据订单id修改订单的状态
    public void changeOrderValue() {
        showOrder();
        System.out.println("请输入您要修改状态的订单id");
        String id = sc.next();
        Order order = o.findByID(id);
        if (order == null) {
            System.out.println("没有当前id的订单，请检查输入");
        } else {
            System.out.println("已找到当前id订单" + order);
            System.out.println("请输入您要修改的状态：0:未支付 1：已支付 2：配送中 3：已完成");
            int value = sc.nextInt();
            String str;
            if (value == 0) {
                str = "未支付";
            } else if (value == 1) {
                str = "已支付";
            } else if (value == 2) {
                str = "配送中";
            } else {
                str = "已完成";
            }
            Order t = new Order(order.getoId(), order.getoTime(), order.getDishes(), order.getoNumber(),
                    order.getTolPrice(), order.getCtm(), order.getSlr(), str);

            o.insert(t);
            System.out.println("修改成功了！！！");
        }
    }

    //显示商家信息
    public void showSlrs()
    {
        List<User> ulist=new ArrayList<>();
        ulist=findSlr();
        if(ulist!=null) {
            for (User user : ulist) {
                if (Objects.equals(user.ID, "商家")) {
                    System.out.println(user);
                }
            }
        }


    }

    //查看所有菜品信息
    public void showAllDishes()
    {
        List<vegetableList>vlist=d.findall();

        for(vegetableList v:vlist)
        {
            System.out.println(v);
        }
    }
    //查看指定类别的菜品信息
    public void showDishesByType()
    {
        System.out.println("请输入您要查询的菜品的类别：");
        String typename=sc.next();
        d.selectByType(typename);
    }




    //根据订单id修改订单状态
    public void changeOrderStatus()
    {
        showOrder();
        System.out.println("请输入您要修改的订单id");
        String oid=sc.next();
        System.out.println("请输入您要修改的状态：（已付款）（已完成）");
        o.findByID(oid).setStatus(sc.next());
    }
    //删除指定id的菜品
    public void deleteDish()
    {
        d.findall();
        System.out.println("请输入您要删除的菜品id：");
        String id=sc.next();
        System.out.println("菜品信息"+d.findByID(id));
        d.delete(id);
        System.out.println("删除成功！");
    }

    /*
    用户的功能喔
    1.注册登录==同商家和管理员
    2.查看店铺，进入店铺查看商品 showByUserId
    3.下单功能
    4.账户余额功能，下单账户余额不足时提示充值
    5.查看订单状态
     */


}
